package com.lins.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.lins.common.Params;
import com.lins.common.util.LinsUtil;
import com.lins.wechat.config.WeChatConfigure;
import com.lins.wechat.dao.RedisDao;
import com.lins.wechat.pojo.DataItem;
import com.lins.wechat.pojo.ResultVo;
import com.lins.wechat.pojo.Weather;
import com.lins.wechat.util.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送微信消息
 * @Author linshuo
 * @Date 2023/7/14 15:33
 */
@Slf4j
@RestController
@RequestMapping("wx")
public class WeChatController {

    @Autowired
    Params params;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    RedisDao redisDao;


    /**
     * 添加用户openId
     **/
    @RequestMapping("setUser")
    public String setUser(String n,String i){
        redisDao.setWxIdAndName(n,i);
        return "设置成功！";
    }

    /**
     * 发送消息入口
     **/
    @RequestMapping("send")
    public String send(String s,String r,String d){
        try {
            //加一层包装，准备提交
            readyPush();

            //携带参数直接提交
//            push(s,r,d);
            return "推送成功";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void readyPush(){
        push("Yn","ls","something…… ","无锡");
    }

    public void push(String sender,String recipient,String diy,String city){

        //根据接收人姓名查询接收人微信号
        String openId = redisDao.findWxIdByName(recipient);

        //维护字体颜色
        Map<String,String> cMap = new HashMap<>();
        cMap.put("A","#00BFFF");
        cMap.put("B","#1f95c5");
        cMap.put("C","#0ace3c");
        cMap.put("D","#dc1010");

        //构建参数
        ResultVo r = ResultVo.initializeResultVo(null==openId?WeChatConfigure.Open_ID:openId, WeChatConfigure.Template_ID,WeChatConfigure.Top_Color);
        //查询天气
//        city = params.getBeijing().get(LinsUtil.merge("[{}]",city));
        city = params.getJiangsu().get(LinsUtil.merge("[{}]",city));
        Weather weather = DataUtils.getWeather(restTemplate,city);

//        Weather weather= null;
        r.set("recipient",new DataItem(recipient,cMap.get("A")));//接收人
        r.set("sender",new DataItem(sender,cMap.get("A")));//发送人
        r.set("diy",new DataItem(diy,cMap.get("A")));

        r.set("date",new DataItem(weather.getDate() + " " + weather.getWeek(),cMap.get("A")));
        r.set("city",new DataItem(weather.getProvince()+" "+weather.getArea(),cMap.get("A")));
        r.set("weather",new DataItem(weather.getWeather(),cMap.get("A")));
        r.set("lowest",new DataItem(weather.getLowest(),cMap.get("A")));//最低气温
        r.set("highest",new DataItem(weather.getHighest(),cMap.get("A")));//最高气温
        r.set("wind",new DataItem(weather.getWind(),cMap.get("A")));//风向
        r.set("windspeed",new DataItem(weather.getWindspeed(),cMap.get("A")));//风速
        r.set("windsc",new DataItem(weather.getWindsc(),cMap.get("A")));//风速等级
        r.set("sunrise",new DataItem(weather.getSunrise(),cMap.get("A")));//日出时间
        r.set("sunset",new DataItem(weather.getSunset(),cMap.get("A")));//日落时间
        r.set("quality",new DataItem(weather.getQuality(),cMap.get("A")));//空气质量
        r.set("alarmlist",new DataItem(weather.getAlarmlist().get(0).getContent(),cMap.get("A")));//气象台
        r.set("tips",new DataItem(weather.getTips(),cMap.get("A")));//温馨提示

        //2.设置日期相关
//        int love_days = DataUtils.getLoveDays(WeChatConfigure.Love_Day);
//        int girl_birthday = DataUtils.getBirthDays(WeChatConfigure.Girl_Birthday);
//        int boy_birthday = DataUtils.getBirthDays(WeChatConfigure.Boy_Birthday);
//        resultVo.setAttribute("love_days",new DataItem(love_days+"","#FFA500"));
//        resultVo.setAttribute("girl_birthday",new DataItem(girl_birthday+"","#FFA500"));
//        resultVo.setAttribute("boy_birthday",new DataItem(boy_birthday+"","#FFA500"));
        //3.设置彩虹屁
//        String rainbow =  DataUtils.getRainbow(restTemplate);
//        resultVo.setAttribute("rainbow",new DataItem(rainbow,"#FF69B4"));
        //4.其他
//        String remark = "❤";
//        resultVo.setAttribute("remark",new DataItem(remark,"#FF1493"));
        //5.给微信服务发送请求，推送微信消息
        String responseStr = restTemplate.postForObject(WeChatConfigure.Send_URL, r, String.class, DataUtils.getAccessToken(restTemplate));
        //6，查看响应日志
        printPushLog(responseStr);
    }

    /**
     * 打印 response log
     * @param responseStr
     */
    private void printPushLog(String responseStr){
        JSONObject jsonObject = JSONObject.parseObject(responseStr);
        String msgCode = jsonObject.getString("errcode");
        String msgContent = jsonObject.getString("errmsg");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("[ {} ] : messageCode={}, messageContent={}",dateFormat.format(new Date()),msgCode,msgContent);
    }
}
